/**
 * 
 */
package com.framework.seedkit.utils.encrypt;

import android.util.Base64;

import java.io.UnsupportedEncodingException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/********************
* DES加解密算法
*********************/

public class Des {
	private static final String TAG = null;
	public static final String PADDING20 = "2020202020202020";

	/**
	 * 描述: 对外提供的加密API
	 * 参数:
	 *   @key 加密key
	 *   @data 加密数据
	 * 返回值: String
	 */
	public static String encptDesECB(String key,String data) throws UnsupportedEncodingException{
		data= Base64Encode(data.getBytes("gbk"));
		data = padding20(data);
		return HexUtil.hex(getDesECBEncCode(key.getBytes(),data.getBytes()));
	}

	/**
	 * 描述: 对外提供的解密API
	 * 参数:
	 *   @key 解密key
	 *   @data 解密数据
	 * 返回值: String
	 */
	public static String decptDesECB(String key,String data) throws UnsupportedEncodingException{
		return new String(Base64Decode(new String(HexUtil.unhexba_1(trim20(HexUtil.hex(getDesECBDesCode(key.getBytes(), HexUtil.unhexba_1(data))))))),"gbk");
	}

	private static String padding20(String data){
		return (data + PADDING20).substring(0, ((data.length()/16)+1)*16);
	}

	private static byte[] padding20byte(String data){
		int length = 0;
		int length2 = 0;

		try {
			length = data.getBytes("gbk").length;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		length2 = ((length/8)+1)*8;
		byte dataByte[] = new byte[length2];
		
		System.arraycopy(data, 0, dataByte, 0, length);
		System.arraycopy(PADDING20.getBytes(), 0, dataByte, length, length2-length);

		return dataByte;
	}

	private static String trim20(String data){
		while(data.endsWith("20")){
			data = data.substring(0, data.length()-2);
		}

		return data;
	}

    private static byte[] getDesECBEncCode(byte[] keyBtyes,byte[] dataBytes) {
        byte[] byteFina = null;
        Cipher cipher;
        try { 
        	SecretKey key = null;
       	  	key = new SecretKeySpec(keyBtyes, "DES");
            cipher = Cipher.getInstance("DES/ECB/NoPadding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byteFina = cipher.doFinal(dataBytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;
    }

	private static byte[] getDesECBDesCode(byte[] key,byte[] byteD) {
        Cipher cipher;
        byte[] byteFina = null;
        try {
        	SecretKey k = null;
       	  	k = new SecretKeySpec(key, "DES");
            cipher = Cipher.getInstance("DES/ECB/NoPadding");//
            cipher.init(Cipher.DECRYPT_MODE, k);
            byteFina = cipher.doFinal(byteD);
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }

        return byteFina;

    }

	public static String Base64Encode(byte[] data) {
		return Base64.encodeToString(data, Base64.DEFAULT);
	}

	public static byte[] Base64Decode(String data) {
		return Base64.decode(data, Base64.DEFAULT);
	}
}
